package come.bjpowernode.javase.reflect;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ReflectTestGetMethodTest {
    public static void main(String[] args) throws FileNotFoundException {
//        InputStream resourceAsStream = ReflectTest051GetFields.class.getResourceAsStream("classInfo.properties");
        String path = Thread.currentThread().getContextClassLoader().getResource("classInfo.properties").getPath();
        FileInputStream fileInputStream = new FileInputStream(path);
        Properties properties = new Properties();
        try {
            properties.load(fileInputStream);
            String className = (String) properties.get("className");
            String method = (String) properties.get("method");

            //根据类的全限定名称获得类对象
            Class aClass = Class.forName(className);
            System.out.println(aClass);
            //根据方法名，获得方法对象
            Method aClassMethod = aClass.getMethod(method,int.class,int.class);
            //获取构造器
            Constructor<?> constructor = aClass.getConstructor();
            //根据构造器，实例化出对象
            Object o = constructor.newInstance();
            //调用对象指定的方法
            aClassMethod.invoke(o, 10,20);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
